<template>
	<view>
		<scroll-view scroll-y class="homeSwiperContainer">
			<water-mark :meta="meta"></water-mark>
			<analysis :subject="curSubject" :dataIndex="dataIndex" :info="info"></analysis>
			<v-analysis :subject="curSubject"></v-analysis>
		</scroll-view>
		<view class="btn" v-if="type != 2">
			<view class="btn-left" @click="lastSub()">
				<text class="btn-textl">上一题</text>
			</view>
			<view class="btn-right" @click="nextSub()">
				<text class="btn-text">下一题</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {isEmpty,showToast} from '@/utils/index';	
    import navbarExam from '@/mixins/common/navbar/exam';
	import vAnalysis from "./fragment/vanalysis.vue";
	import Analysis from "./fragment/Analysis.vue";
	import userInfo from "@/mixins/index";
	import HomeworkService from "@/services/homeworkService";
	const homeworkService = new HomeworkService();
	export default {
        mixins:[navbarExam,userInfo],
		components: {
			vAnalysis,
			Analysis
		},
		computed: {
			subjectDefault() {
				return {
					subjectNum: '',
					type: 1,
					subjectChoices: [],
					content: '',
					answer: '',
					choice:'',
					selectChoice: [],
				}
			},
            curSubject() {
				return this.subjectDetail.length != 0 ? this.subjectDetail[this.dataIndex] : this.subjectDefault
			}
		},
		
		data() {
			return {
				lessonId:'',
				subjectId:'',
				type: 1, //type0错题1全部 2:单题解析
				subjectDetail: [],
				dataIndex: 0,
				subjectSize: 0, //试卷题目总数
				realSubjectSize: 0,
				choiceType: 1, // 1 单选 2 多选
				searchIndex: 0,
				info:{
					subjectSize:''
				},
                examBarData: [{
                    img: '/static/exam/examBar01.png',
                    text: '纠错',
                    id: 'findError1',
                    type: 'static',
                    onPress: (context) => {
                        //判断用户是否禁言，0未禁言，1禁言
                        if(context.userData.banSay == 0){
                            uni.navigateTo({
                                url:'/pages/exam/errorCollection?subjectId='+context.curSubject.subjectId
                            })
						}else{
                            context.tip("你已经被禁言，禁止纠错")
						}
                    }
                }]
				}
		},
		onLoad(query) {
			this.type = query.type;
			this.lessonId = query.lessonId;
			this.subjectId = query.subjectId;
			this.info.name = query.name;
			if(this.type=='2'){
				this.getOneAnaly()
			}else{
				this.getAnaly();
			}
		},
		methods: {
			test(){
				uni.navigateTo({
				    url:'/pages/exam/errorCollection?subjectId='+this.curSubject.subjectId
				})
			},
			//获取解析数据（全部解析/错题解析）
			getAnaly() {
				const {
					lessonId,
					type
				} = this;
				homeworkService.homeworkAnalysis({lessonId,type}).then(res=>{
					this.loadDataCallback(res);
				})
			},
			//单题解析
			getOneAnaly(){
				const {
					lessonId,
					subjectId
				} = this;
				homeworkService.homeworkOneAnalysis({lessonId,subjectId}).then(res=>{
					this.loadDataCallback(res)
				})
			},
			loadDataCallback(res){
				if(res.success){
					if(res.result.detail.length == 0){
						this.tip("恭喜你无错题")
						uni.navigateBack();
					}
					this.subjectDetail = res.result.detail
					
					this.info.subjectSize = res.result.total;
					this.realSubjectSize = this.subjectDetail.length
				}else{
					showToast(res.message)
				}
			},
			lastSub() {
				if (this.dataIndex != 0) {
					//展示前面一题
					this.dataIndex--
					this.choiceType = this.subjectDetail[this.dataIndex].type
				} else {
					this.tip("已经是第一题了")
				}
			},
			nextSub() {
				if (this.dataIndex != (Number(this.realSubjectSize) - 1)) {
					this.dataIndex++
					this.choiceType = this.subjectDetail[this.dataIndex].type
				} else {
					this.tip("已经是最后一题了")
				}

			},
			tip(mes) {
				uni.showToast({
					title: mes,
					duration: 2000,
					icon: 'none'
				});
			},
			showModel(mes) {
				if (mes != undefined) {
					uni.showModal({
						title: '提示',
						content: mes
					});
				}
			}
		},
	}
</script>

<style lang='scss' scoped>
	.homeSwiperContainer {
		padding-bottom: 80upx;
		height: calc(100vh-80upx);
	}

	.btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		text-align: center;
		line-height: 80upx;

		&-left {
			display: inline-block;
			width: 50%;
			height: 80upx;
			background: #fff;
			box-shadow: 0 -1upx 10upx #ccc;
		}

		&-right {
			display: inline-block;
			width: 50%;
			height: 80upx;
			background: #5ea1f8;
			box-shadow: 0 -1upx 10upx #ccc;
		}

		&-textl {
			font-size: 28upx;
			color: #333;
		}

		&-text {
			font-size: 28upx;
			color: #fff;
		}
	}
</style>

<style lang='scss'>
	page {
		height: 100%;
	}
</style>
